package com.wei.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.entity.Course;
import com.wei.entity.vo.CourseInfo;
import com.wei.entity.vo.CoursePublishVo;
import com.wei.entity.vo.CourseQueryVo;
import com.wei.service.CourseService;
import com.wei.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wjx
 * @ClassName CourseController
 * @description: TODO
 * @date 2022-11-29 21:15
 * @version: 1.0
 */
@RestController
@RequestMapping("/eduservice/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @PostMapping("/saveCourseInfo")
    public R saveCourseInfo(@RequestBody CourseInfo courseInfo) {
        String cid = courseService.addCourseInfo(courseInfo);

        return R.ok().data("cid", cid);
    }

    @PostMapping("/addCourse")
    public R addCourse(@RequestBody Course course) {
        boolean save = courseService.save(course);

        return R.ok().data("cid", save);
    }

    @GetMapping("/findAll")
    public R findAll() {
        List<Course> list = courseService.list();
        return R.ok().data("lists", list);
    }

    /**
     * 根据课程id查询课程信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getCourseInfoById/{id}")
    public R getCourseInfoById(@PathVariable Long id) {

        CourseInfo courseInfo = courseService.queryCourseInfo(id);

        return R.ok().data("courseInfo", courseInfo);
    }

    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfo courseInfo) {

        courseService.updateCourseData(courseInfo);

        return R.ok();
    }

    /**
     * 课程发布信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("/getCoursePublishInfo/{courseId}")
    public R getCoursePublishInfo(@PathVariable String courseId) {
        CoursePublishVo publishInfo = courseService.getPublishInfo(courseId);

        return R.ok().data("publishInfo", publishInfo);
    }

    /**
     * 发布课程
     *
     * @param courseId
     * @return
     */
    @GetMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId) {
        courseService.publishCourse(courseId);

        return R.ok();
    }

    @GetMapping("/getAllCourse")
    public R getAllCourse() {
        List<Course> list = courseService.list();

        return R.ok().data("list", list);
    }

    /**
     * 条件查询带分页
     *
     * @param current
     * @param limit
     * @param courseQueryVo
     * @return
     */
    @PostMapping("/getCoursePage/{current}/{limit}")
    public R getCoursePage(@PathVariable Long current,
                           @PathVariable Long limit,
                           @RequestBody CourseQueryVo courseQueryVo) {

        Page<Course> page = new Page<>(current, limit);

        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();

        String title = courseQueryVo.getTitle();
        String status = courseQueryVo.getStatus();
        String begin = courseQueryVo.getBegin();
        String end = courseQueryVo.getEnd();

        queryWrapper.like(StrUtil.isNotBlank(title), Course::getTitle, title)
                .eq(StrUtil.isNotBlank(status), Course::getStatus, status)
                .ge(StrUtil.isNotBlank(begin), Course::getGmtCreate, begin)
                .le(StrUtil.isNotBlank(end), Course::getGmtCreate, end);

        courseService.page(page, queryWrapper);

        List<Course> records = page.getRecords();
        long total = page.getTotal();

        return R.ok().data("records", records).data("total", total);
    }

    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {

        courseService.deleteCourse(courseId);

        return R.ok();
    }
}
